#!/usr/bin/perl
# $Id$
#
# Author: Charles Mauch (cmauch@gmail.com)
# Description: Modules to handle email arrival announcement
# Legal: (c) 2006 Charles Mauch, released under the terms of the GPLv2 or later

# -- [ sub: JabberAnnounce  ] -----------------------------------------------
#
#Summary:      Subroutine to call Jabber, an IM protocol, to announce an emails
#              arrival.
#Parameters:   email address, full name, email subject
#Return:       none

sub JabberAnnounce {
    my ( $address, $username, $subject ) = @_;

    use Net::Jabber;
    use Net::Jabber::Client;
    my $jabconn = Net::Jabber::Client->new();
    $jabconn->Connect( "hostname" => "jabber.mauch.name", "port" => 5222 ) or die "Cannot connect ($!)\n";

    my @result = $jabconn->AuthSend( "username" => 'bot',
                                     "password" => 'botsy',
                                     "resource" => 'email'
                                   );

    my $msg = Net::Jabber::Message->new();
    $msg->SetMessage( "to"      => "charles\@jabber.mauch.name",
                      "subject" => "New Email has arrived!",
                      "body"    => "From: $username at $address\nSubject: $subject"
                    );
    $jabconn->Send( $msg );
    $jabconn->Disconnect();
} ## end sub JabberAnnounce

# -- [ sub: FestivalAnnounce  ] ---------------------------------------------
#
#Summary:      Subroutine to call festival, a text to speech program, to
#              announce email's arriving, between particular hours of the day.
#Parameters:   full name
#Return:       none

sub FestivalAnnounce {
    my $username = shift;

    # Currently, Drops Sat/Sun, allows 9am thru 6pm.

    my ( $hour, $wday ) = ( localtime )[ 2, 6 ];
    if ( $wday != 0 and $wday != 6 and $hour > 9 and $hour < 18 ) {
        my @commandargs = "echo \"New e-mail from $username\" | flite";
        system @commandargs;
    }
} ## end sub FestivalAnnounce

1;
